#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <MCDrawLineSegmentCommand.h>
#include <MCDrawCircleCommand.h>
#include <MCDrawEllipseCommand.h>
#include <MCDrawArcCommand.h>
#include <MCDrawLinearDimensionCommand.h>
#include <qmessagebox.h>

#include <MCIncidentConstraint.h>
#include <MCParallelConstraint.h>
#include <MCPerpendicularConstraint.h>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _canvas = new MCCanvas(this);
    _canvas->setGeometry(ui->graphicsView->geometry());
    ui->graphicsView = _canvas;

    _commandManager = _canvas->commandManager();
    _selectController = _canvas->selectController();
    _constraintManager = _canvas->constraintManager();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_drawLineSegment_clicked()
{
    MCDrawLineSegmentCommand *command = new MCDrawLineSegmentCommand(_commandManager);
    _commandManager->setCommand(command);
}


void MainWindow::on_drawEllipse_clicked()
{
    MCDrawEllipseCommand *command = new MCDrawEllipseCommand(_commandManager);
    _commandManager->setCommand(command);
}


void MainWindow::on_drawCircle_clicked()
{
    MCDrawCircleCommand *command = new MCDrawCircleCommand(_commandManager);
    _commandManager->setCommand(command);
}


void MainWindow::on_drawArc_clicked()
{
    MCDrawArcCommand *command = new MCDrawArcCommand(_commandManager);
    _commandManager->setCommand(command);
}


void MainWindow::on_drawLinearDimension_clicked()
{
    MCDrawLinearDimensionCommand *command = new MCDrawLinearDimensionCommand(_commandManager);
    _commandManager->setCommand(command);
}


void MainWindow::on_cnstr_incident_clicked()
{
    std::vector<MCGripPoint *> gripPoints = _selectController->selectedGripPoints();

    if (gripPoints.size() == 2)
    {
        MCIncidentContraint *c = new MCIncidentContraint(gripPoints[0], gripPoints[1]);
        _constraintManager->addConstraint(c);
    }
    else
    {
        QMessageBox messageBox1;
        messageBox1.setText("Failded\nChoose 2 grips");
        messageBox1.exec();
    }
}


void MainWindow::on_cnstr_parallel_clicked()
{
    std::vector<IGraphicsItem *> selections = _selectController->selections();
    

    if (selections.size() == 2)
    {
        if (dynamic_cast<MCSegmentVisual *>(selections[0]) && dynamic_cast<MCSegmentVisual *>(selections[1]))
        {
            MCSegmentVisual *segv1 = dynamic_cast<MCSegmentVisual *>(selections[0]);
            MCSegmentVisual *segv2 = dynamic_cast<MCSegmentVisual *>(selections[1]);

            MCParallelConstraint *c = new MCParallelConstraint(segv1, segv2);
            _canvas->constraintManager()->addConstraint(c);
        }
        else
        {
            QMessageBox messageBox2;
            messageBox2.setText("Failded\nChoose only line segments");
            messageBox2.exec();
        }
    }
    else
    {
        QMessageBox messageBox1;
        messageBox1.setText("Failded\nChoose 2 items");
        messageBox1.exec();
    }
}


void MainWindow::on_cnstr_perpendicular_clicked()
{
    std::vector<IGraphicsItem *> selections = _selectController->selections();

    if (selections.size() == 2)
    {
        if (dynamic_cast<MCSegmentVisual *>(selections[0]) && dynamic_cast<MCSegmentVisual *>(selections[1]))
        {
            MCSegmentVisual *segv1 = dynamic_cast<MCSegmentVisual *>(selections[0]);
            MCSegmentVisual *segv2 = dynamic_cast<MCSegmentVisual *>(selections[1]);

            MCPerpendicularConstraint *c = new MCPerpendicularConstraint(segv1, segv2);
            _canvas->constraintManager()->addConstraint(c);
        }
        else
        {
            QMessageBox messageBox2;
            messageBox2.setText("Failded\nChoose only line segments");
            messageBox2.exec();
        }
    }
    else
    {
        QMessageBox messageBox1;
        messageBox1.setText("Failded\nChoose 2 items");
        messageBox1.exec();
    }
}

